/**
 * Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
 * All rights reserved.
 *
 * For the applicable distribution terms see the license.txt -file, included in
 * the distribution.
 */

#ifndef DECLARATIVEPURCHASELISTMODEL_H
#define DECLARATIVEPURCHASELISTMODEL_H

#include <QAbstractListModel>
#include <QDeclarativeParserStatus>
#include <QImage>
#include <iapclient.h>

class PurchasableItemBase;

/**
 * @class DeclarativePurchaseListModel
 *
 * @brief Implementents Catalog list model
 *
 */
class DeclarativePurchaseListModel : public QAbstractListModel, public QDeclarativeParserStatus
{
    Q_OBJECT
    Q_INTERFACES(QDeclarativeParserStatus)

public:
    Q_PROPERTY(bool drmFilter READ drmFilter WRITE setDrmFilter)
    Q_PROPERTY(bool isApplicationBusy READ isApplicationBusy NOTIFY applicationBusyChanged)

    /**
     * @see QAbstractListModel
     *
     * @brief base class abstract method implementation
     *
     * @param parent
     */
    int rowCount(const QModelIndex & parent = QModelIndex()) const;
    /**
     * @see QAbstractListModel
     *
     * @brief base class abstract method implementation
     *
     * @param index
     * @param role
     */
    QVariant data(const QModelIndex &index, int role) const;
    /**
     * @see QAbstractListModel
     *
     * @brief base class abstract method implementation
     *
     * @param index
     * @param value
     * @param role
     */
    bool setData (const QModelIndex & index, const QVariant & value, int role = Qt::EditRole);

    //property accesors
    /**
     * @brief
     *
     */
    bool drmFilter() const;
    /**
     * @brief
     *
     * @param val
     */
    void setDrmFilter(bool val);
    /**
     * @brief
     *
     */
    bool isApplicationBusy() const;

    /**
     * @brief
     *
     * @param index
     */
    Q_INVOKABLE void buy(int index);
    /**
     * @brief
     *
     */
    Q_INVOKABLE void restoreProducts();
    /**
     * @brief
     *
     */
    Q_INVOKABLE void getUserInfo();

    /**
     * @brief
     *
     */
    Q_INVOKABLE void purgeProducts();

public:
    Q_DISABLE_COPY(DeclarativePurchaseListModel)
    /**
     * @brief
     *
     * @param
     */
    explicit DeclarativePurchaseListModel(QObject *parent = 0);
    /**
     * @brief
     *
     */
    ~DeclarativePurchaseListModel();

    //QDeclarativeParserStatus interface
    /**
     * @brief
     *
     */
    void classBegin();
    /**
     * @brief
     *
     */
    void componentComplete();

public:
    // for model items to use only
    /**
     * @brief
     *
     */
    void beginModelUpdate();
    /**
     * @brief
     *
     */
    void commitModelUpdate();
    /**
     * @brief
     *
     */
    IAPClient& client();
    /**
     * @brief
     *
     * @param index
     */
    PurchasableItemBase* record(int index) const;

signals:
    /**
     * @brief
     *
     */
    void applicationBusyChanged();

protected slots:
    /*
     * In-Application Purchase specific slots
     */
    /**
     * @brief
     *
     * @param requestId
     * @param status
     * @param items
     */
    void restorableProductsReceived( int requestId, QString status,
                                     IAPClient::ProductDataList items);
    /**
     * @brief
     *
     * @param requestId
     * @param status
     * @param userdata
     */
    void userAndDeviceDataReceived (int requestId, QString status,
                                    IAPClient::UserAndDeviceDataHash userdata);

private:
    //utility functions
    /**
     * @brief
     *
     */
    void readNonProtectedCatalog();
    /**
     * @brief
     *
     */
    void readDRMCatalog();
private:
    // In-Application Purchase API
    IAPClient                       m_client; /**< TODO */

    //for aggregate query to product list getRestorableProducts()
    int                             m_requestId; /**< TODO */

    QList<PurchasableItemBase*>     m_data; /**< TODO */
    int                             m_modelCommitRefCounter; /**< TODO */

    // enables the whole application busy indicator
    bool                            m_isApplicationBusy; /**< TODO */
};

#endif // DECLARATIVEPURCHASELISTMODEL_H
